package com.job.project.controller;

import com.job.project.components.HttpClientUtils;
import com.job.project.domain.Your;
import com.job.project.event.MyEvent;
import com.job.project.service.AsyncService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author 王博
 * @DateTime 2024-05-24 15:30
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private HttpClientUtils httpClientUtils;
    @Resource
    private AsyncService asyncDemo;
    @Resource
    private ApplicationContext applicationContext;
    @Resource
    private RabbitTemplate rabbitTemplate;
    @Value("${snGenerator.prefix:}")
    private String prefix;

    @GetMapping("/forGet")
    public String getUser() throws Exception{
        System.out.println(prefix);
        Map<String,String>  params = new HashMap<>();
        params.put("name","zhangsan");
        params.put("address","河南");

        String result = httpClientUtils.doGet("product/get",params);
        log.info(result);
        return result;
    }

    @GetMapping("/forAsyncOne")
    public String forAsyncOne(){
        System.out.println("one:"+Thread.currentThread().getName());
        System.out.println("one:"+Thread.currentThread().getThreadGroup().getName());
        asyncDemo.asyncForOne();
        return " forAsyncOne";
    }

    @GetMapping("/forAsyncTwo")
    public String forAsyncTwo(){
        System.out.println("two:"+Thread.currentThread().getName());
        System.out.println("two:"+Thread.currentThread().getThreadGroup().getName());
        asyncDemo.asyncForTwo();
        return " forAsyncTwo";
    }

    @GetMapping("/forSync")
    public String forSync(){
        System.out.println("sync:"+Thread.currentThread().getName());
        System.out.println("sync:"+Thread.currentThread().getThreadGroup().getName());
        asyncDemo.sync();
        return " forSync";
    }

    @GetMapping("/forEvent")
    public String forEvent() {
        System.out.println(LocalDateTime.now()+":forEvent:"+Thread.currentThread().getName());
        System.out.println(LocalDateTime.now()+":forEvent:"+Thread.currentThread().getThreadGroup().getName());
        for (int i = 0; i < 5; i++) {
            applicationContext.publishEvent(new MyEvent("我就是事件里的内容:"+i));
        }
        System.out.println(LocalDateTime.now()+":forEent:发完了，发完了。发完了…………………………");
        return " forEvent";
    }


    @PostMapping("/hand")
    public String hand(@RequestBody Your user){
        System.out.println(user);
        return "hand";
    }


    @GetMapping("/sendMes")
    public String sendMes(@RequestParam String mes){
        for (int i =0;i<10;i++){
            rabbitTemplate.convertAndSend("myExchange",null, mes+i);
        }
        return "成功了";
    }


}
